/**
 * Belongs to WEF
 */

Namespace = new Object();
Namespace.register = function(fullNS) {
	var nsArray = fullNS.split('.');
	var sEval = "";
	var sNS = "";
	for (var i = 0; i < nsArray.length; i++) {
		if (i != 0)
			sNS += ".";
		sNS += nsArray[i];
		sEval += "if (typeof(" + sNS + ") == 'undefined') " + sNS
				+ " = new Object();"
	}
	if (sEval != "")
		eval(sEval);
}
var selectRowIds = [];
var CommonUtils = {
	autoHeight : function() {
		document.getElementById("ModuleLeft").style.height = "";
		document.getElementById("ModuleRight").style.height = "";
		var Left_height = document.getElementById("ModuleLeft").scrollHeight;
		var Right_height = document.getElementById("ModuleRight").scrollHeight
				- 10;
		layoutHeight = Math.max(Left_height, Right_height);
		var min_height = document.body.clientHeight - 146;
		if (layoutHeight < min_height) {
			layoutHeight = min_height;
		}
		if (navigator.userAgent.indexOf("MSIE") > 0) {
			document.getElementById("ModuleLeft").style.height = layoutHeight
					+ 20 + "px";
		} else {
			document.getElementById("ModuleLeft").style.height = layoutHeight
					+ "px";
		}
		document.getElementById("ModuleRight").style.height = layoutHeight
				+ "px";
		document.getElementById("ModuleToggle").style.height = layoutHeight
				+ 10 + "px";
	},
	getMessage : function(string) {
		try {
			return eval(string);
		} catch (e) {
			return string;
		}
	},
	/* added by liury */
	getClip : function() {
		var clipData;
		if (window.clipboardData) {
			clipData = window.clipboardData.getData('text');
			// alert('IE');
		} else {
			if (window.netscape) {
				try {
					netscape.security.PrivilegeManager
							.enablePrivilege('UniversalXPConnect');
					var clip = Components.classes["@mozilla.org/widget/clipboard;1"]
							.createInstance(Components.interfaces.nsIClipboard);
					if (!clip) {
						alert('can\'t do clip');
						return 'null';
					}

					var trans = Components.classes["@mozilla.org/widget/transferable;1"]
							.createInstance(Components.interfaces.nsITransferable);
					if (!trans) {
						return 'null';
					}
					trans.addDataFlavor('text/unicode');
					clip.getData(trans, clip.kGlobalClipboard);
					var str = new Object();
					var len = new Object();
					trans.getTransferData('text/unicode', str, len);
				} catch (e) {
					return 'null';
				}
				if (str) {
					if (Components.interfaces.nsISupportsWString) {
						str = str.value
								.QueryInterface(Components.interfaces.nsISupportsWString);
					} else {
						if (Components.interfaces.nsISupportsString) {
							str = str.value
									.QueryInterface(Components.interfaces.nsISupportsString);
						} else {
							str = null;
						}
					}
				}
				if (str) {
					clipData = str.data.substring(0, len.value / 2);
				}
			}
		}
		return clipData;
	},
	/*
	 * added by liury
	 */
	clearClip:function(){
		if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){  
	         if (window.clipboardData){  
	             window.clipboardData.clearData();}  
	         else if (window.netscape){  
	             //alert(typeof(netscape));  
	             try{  
	                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');  
	             } catch (e) {  
	                 alert("您的firefox安全限制限制您进行剪贴板操作，请打开about:config将signed.applets.codebase_principal_support设置为true之后重试");  
	             }  
	             var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard); 
	             if (!clip) return; 
	             var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable); 
	             if (!trans) return; 
	             trans.addDataFlavor('text/unicode');  
	             var str = new Object();  
	             var len = new Object();  
	             var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);  
	             var texttext='';  
	             str.data=texttext;  
	             trans.setTransferData("text/unicode",str,texttext.length*2);  
	             var clipid=Components.interfaces.nsIClipboard;  
	             if (!clip) return false;  
	             clip.setData(trans,null,clipid.kGlobalClipboard);  
	         }  
	   } else { 
	   			window.clipboardData.clearData(); 
	   }  
	     return false;  
	},
	checkNumberTextInput : function(event, obj, positive, intPart, decPart) {
		var originalValue = obj.value.replace(/,/g, ''); // edit by chencx
		if (event.keyCode) {
			if (event.keyCode != 9
					&& event.keyCode != 11
					&& event.keyCode != 35
					&& event.keyCode != 36 // END/HOME
					&& event.keyCode != 37
					&& event.keyCode != 38 // 方向键
					&& event.keyCode != 39
					&& event.keyCode != 40 // 方向键
					&& event.keyCode != 45 && event.keyCode != 46
					&& event.keyCode != 127 && event.keyCode != 8
					&& (event.keyCode < 48 || event.keyCode > 57)) {
				// ie6,7,8,opera,chrome管用
				return false;
			} else if (event.keyCode == 9 || event.keyCode == 11
					|| event.keyCode == 35 || event.keyCode == 36
					|| event.keyCode == 37 || event.keyCode == 38
					|| event.keyCode == 39 || event.keyCode == 40
					|| event.keyCode == 127 || event.keyCode == 8 ) {//去掉46,避免输入多个小数点
				// Do Nothing:功能键无需进行详细判断，直接可用。
			} else {
				var selectStart = CommonUtils.getSelectionStart(obj);
				var selectEnd = CommonUtils.getSelectionEnd(selectStart, obj);
				originalValue = originalValue.substring(0, selectStart)
						+ originalValue.substring(selectEnd,
								originalValue.length);
				if (originalValue != null && originalValue != undefined
						&& originalValue.length != 0) {
					// 输入减号
					if (event.keyCode == 45) {
						if (positive) {
							return false;
						}
						if (originalValue.indexOf('-') == 0) {
							if (selectStart != 0 || selectEnd == 0) {
								return false;
							}
						} else {
							if (selectStart != 0) {
								return false;
							}
						}
						return true;
					} else if (event.keyCode == 46) {// 输入小数点
						if (originalValue == '-'
								|| originalValue.indexOf('.') != -1
								|| selectEnd == originalValue.indexOf('-') + 1
								|| decPart == 0) {
							return false;
						}
						return true;
					} else if (originalValue.indexOf('.') == -1) {// 没有小数点，限制整数部分
						if (((originalValue.indexOf('-') != -1 && originalValue.length == intPart
								+ 1) || (originalValue.indexOf('-') == -1 && originalValue.length == intPart))
								&& event.keyCode != 46) {
							return false;
						}
					} else {
						if (originalValue.indexOf('-') == -1) {
							var strIntPart = originalValue.substring(0,
									originalValue.indexOf('.'));
						} else {
							var strIntPart = originalValue.substring(1,
									originalValue.indexOf('.'));
						}

						if (selectStart <= originalValue.indexOf('.')) {
							if (strIntPart.length == intPart) {
								return false;
							}
						} else {
							var strDecPart = originalValue.substring(
									originalValue.indexOf('.') + 1,
									originalValue.length);
							if (strDecPart.length == decPart) {
								return false;
							}
						}
					}
				} else if (event.keyCode == 46
						|| (event.keyCode == 45 && positive)) {
					return false;
				}
			}
		} else {
			if (event.charCode
					&& (event.charCode != 9 && event.charCode != 11
							&& event.charCode != 35
							&& event.charCode != 36 // END/HOME
							&& event.charCode != 37
							&& event.charCode != 38 // 方向键
							&& event.charCode != 39
							&& event.charCode != 40 // 方向键
							&& event.charCode != 45 && event.charCode != 46
							&& event.charCode != 127 && event.charCode != 8 && (event.charCode < 48 || event.charCode > 57))) {
				// firefox管用
				event.preventDefault();
			} else if (event.charCode == 0 || event.charCode == 9
					|| event.charCode == 11 || event.charCode == 35
					|| event.charCode == 36 || event.charCode == 37
					|| event.charCode == 38 || event.charCode == 39
					|| event.charCode == 40 || event.charCode == 127
					|| event.charCode == 8) {
				// Do Nothing:功能键无需进行详细判断，直接可用。
			} else {
				originalValue = originalValue.substring(0, obj.selectionStart)
						+ originalValue.substring(obj.selectionEnd,
								originalValue.length);
				if (originalValue != null && originalValue != undefined
						&& originalValue.length != 0) {
					// 输入减号
					if (event.charCode == 45) {
						if (positive) {
							event.preventDefault();
						}
						if (originalValue.indexOf('-') == 0) {
							if (obj.selectionStart != 0
									|| obj.selectionEnd == 0) {
								event.preventDefault();
							}
						} else {
							if (obj.selectionStart != 0) {
								event.preventDefault();
							}
						}
						return true;
					} else if (event.charCode == 46) { // 输入小数点
						if (originalValue == '-'
								|| originalValue.indexOf('.') != -1
								|| obj.selectionEnd == originalValue
										.indexOf('-')
										+ 1 || decPart == 0) {
							event.preventDefault();
						}
						return true;
					} else if (originalValue.indexOf('.') == -1) {// 没有小数点，限制整数部分
						if (((originalValue.indexOf('-') != -1 && originalValue.length == intPart
								+ 1) || (originalValue.indexOf('-') == -1 && originalValue.length == intPart))
								&& event.charCode != 46) {
							event.preventDefault();
						}
					} else {
						if (originalValue.indexOf('-') == -1) {
							var strIntPart = originalValue.substring(0,
									originalValue.indexOf('.'));
						} else {
							var strIntPart = originalValue.substring(1,
									originalValue.indexOf('.'));
						}

						if (obj.selectionStart <= originalValue.indexOf('.')) {
							if (strIntPart.length == intPart) {
								event.preventDefault();
							}
						} else {
							var strDecPart = originalValue.substring(
									originalValue.indexOf('.') + 1,
									originalValue.length);
							if (strDecPart.length == decPart) {
								event.preventDefault();
							}
						}
					}
				} else if (event.charCode == 46
						|| (event.charCode == 45 && positive)) {
					event.preventDefault();
				}
			}
		}
	},

	/**
	 * @summary 千分位表示法：以逗号隔开的千分位表示法显示数字 add by chencx
	 * @para v 输入参数
	 * @para isReplaceSpliter
	 */
	getScienceNumber : function(v, isReplaceSpliter) {
		v += '';
		var minus = ''; // 负号
		var spliter = ',' // 千分位分割符

		if (isReplaceSpliter) {
			v = v.replace(/,/g, '');
		}

		// 判断是否是负数
		if (v.indexOf('-') != -1) {
			minus = '-';
			v = v.substr(1);
		}
		var decimal = ''; // 小数部分
		var integer = ''; // 整数部分
		var pIndex = v.indexOf('.');
		if (pIndex != -1) {
			decimal = v.substr(pIndex);
			v = v.substring(0, pIndex);
		}

		var count = v.length;
		var remain = count % 3;
		if (remain == 0) {
			for (var i = 0; i < count; i = i + 3) {
				integer += v.substr(i, 3);
				if (count != i + 3) {
					integer += spliter;
				}
			}
		} else {
			if (count > 3) {
				integer += v.substr(0, remain);
				integer += spliter;

				for (var i = remain; i < count; i = i + 3) {
					integer += v.substr(i, 3);
					if (count != i + 3) {
						integer += spliter;
					}
				}
			} else {
				integer = v;
			}
		}
		return minus + integer + decimal;
	},

	getSelectionStart : function(obj) {
		if (obj.document) {
			var curRange = obj.document.selection.createRange();// 取得当前光标所在位置的TextRange
			var tbRange = obj.createTextRange();// 整个输入框的TextRange
			tbRange.collapse(true);// 将光标移到头部(TextRange的头与尾的位置相等并等于头部值)
			tbRange.select();// 执行一次选择,改变document.selection的内容，collapse同时移动头和尾，实际上返回的是空的TextRange。
			var headRange = obj.document.selection.createRange();// 取得头部的TextRange
			headRange.setEndPoint("EndtoEnd", curRange);// 尾部与开始光标的尾部相同。
			var pos = headRange.text.length;
			curRange.select();// 恢复光标位置
			return pos - curRange.text.length;
		}
	},
	getSelectionEnd : function(posStart, obj) {
		if (obj.document) {
			var curRange = obj.document.selection.createRange();// 取得当前光标所在位置的TextRange
			return posStart + curRange.text.length;
		}
	},
	returnCode : function(codeName) {
		var strCode="";
		var codeNameArr=codeName.split(',');
		for(var i=0;i<codeNameArr.length;i++){
			var codeName=codeNameArr[i];
			if (codeName && codeName.indexOf('/') != -1) {
				strCode += codeName.substring(0, codeName.indexOf('/'))+'/';
			}else{
				strCode+=codeNameArr[i]+'/';
			}
		}
		if(strCode!='')
			strCode=strCode.substring(0,strCode.length-1);
		return strCode;
	},
	// ECTABLE中checkbox选择框专用
	// @author guoxl
	// @date 2009-06-12
	setSingleCheckBoxStatus : function(chkbx) {
		// make sure that always know the state of the checkbox
		if (chkbx.checked) {
			$("input[name*='" + chkbx.name + "']").val('1');
			if (chkbx.name.indexOf('chkbx_') != -1) {
				if (selectRowIds[0]
						&& selectRowIds[0].substring(0, selectRowIds[0]
										.lastIndexOf("_")) != chkbx.name
								.substring(0, chkbx.name.lastIndexOf("_"))) {
					selectRowIds = [];
				}
				selectRowIds.push(chkbx.name);
			}
		} else {
			$("input[name*='" + chkbx.name + "']").val('0');
			$("input[name='selectAll']").removeAttr('checked');
			selectRowIds.pop(chkbx.name);
		}
	},
	setAllCheckBoxesStatus : function(headerChk) {
		var form = headerChk.form;
		if ($(headerChk).attr('checked') == true) {
			for (var i = 0; i < form.length; i++) {
				var jObj = $(form[i]);
				if (jObj.attr('type') == 'checkbox') {
					jObj.attr('checked', 'true');
					$(form[i - 1]).val('1');
					if (jObj.attr('name').indexOf('chkbx_') != -1) {
						if (selectRowIds[0]
								&& selectRowIds[0].substring(0, selectRowIds[0]
												.lastIndexOf("_")) != jObj
										.attr('name').substring(
												0,
												jObj.attr('name')
														.lastIndexOf("_"))) {
							selectRowIds = [];
						}
						selectRowIds.push(jObj.attr('name'));
					}
				}
			}
		} else {
			for (var i = 0; i < form.length; i++) {
				var jObj = $(form[i]);
				if (jObj.attr('type') == 'checkbox') {
					jObj.removeAttr('checked');
					$(form[i - 1]).val('0');
					selectRowIds.pop(jObj.attr('name'));
				}
			}
		}
	},
	maxLength : function(field, maxlimit) {
		var j = field.value.len();
		if (j > maxlimit) {
			var tt = "";
			var tempString = field.value;
			for (var i = 0; i < maxlimit; i++) {
				if (tt.len() < maxlimit)
					tt = tempString.substr(0, i + 1);
				else
					break;
			}
			if (tt.len() > maxlimit)
				tt = tt.substr(0, tt.length - 1);
			field.value = tt;
			delete tempString;
			delete tt;
		}
	},
	calculateTotal : function(inputName,tableId,tdIndex,formatStr){
		var inputObjs = $("input[name='"+inputName+"'][type='text']",document.getElementById(tableId));
		var totalValue = 0;
		inputObjs.each(function(index,inputObj){
			if(parseFloat(inputObj.value))
				totalValue += parseFloat(inputObj.value);
		});
		if(formatStr && formatStr !=''){
			var pos = formatStr.length - 1;
			totalValue = Math.round(totalValue*Math.pow(10, pos))/Math.pow(10, pos);
		}else{
			totalValue = parseInt(totalValue);
		}
		var rows = $("#" + tableId + " tr.calcRow");
		rows.each(function(index,row){
			var rowWidth = row.cells[tdIndex].clientWidth-7;
			row.cells[tdIndex].innerHTML = "<div title='"+totalValue+"' style='width:"+
				rowWidth+"px;' class='EcTableColumnEllipsis' onmouserover=\"title='"+
				totalValue+"'\">"+totalValue+"</div>";
		});
	} ,
	//2010-04-13 jcc 千分位控件赋值
	setCommaValue : function(id , value){
		$('#' + id + '_code_nama_showComma').val(CommonUtils.getScienceNumber(value)) ;
		$('#' + id).val(value) ;
	}
}



var ListUtils={
		showItemSel:function(valueFrom,textId,collection,delimiter,title){//创建list弹出框
			delimiter=(delimiter==null||delimiter==''||delimiter=='null'||delimiter=='NULL')?',':delimiter;
			var objWindow = {};
			var typeValues;
			objWindow.selOption=[];
			objWindow.allOption=[];
			objWindow.titleText=title;
			objWindow.valueFrom=valueFrom;
			typeValues=document.getElementById(textId).value.split(delimiter);
			collection=collection.substring(1,collection.length-1);
			var array=collection.split('],[');
			for(var i=0;i<array.length;i++){
				var temp=array[i].split(',');
				objWindow.allOption[i]={value:temp[0],textValue:temp[1]};
			}
			if(typeValues[0]!=''){
				for(var j=0;j<typeValues.length;j++){
					objWindow.selOption[objWindow.selOption.length]={value:typeValues[j],textValue:typeValues[j]};
				}
			}
			var returnValue = window.showModalDialog(App.ctx+'/pages/common/listSelector.jsp', objWindow, 
					'dialogWidth:410px;dialogHeight:300px;status:no;');
			var textValues ='';
			var values ='';
			if(returnValue){
				for(var i=0;i<returnValue.length;i++){
					textValues=textValues+returnValue[i].textValue;
					values=values+returnValue[i].value;
					if(i!=returnValue.length-1){
						textValues=textValues+delimiter;
						values=values+delimiter;
					}						
				}
			}
			document.getElementById(textId).value=textValues;
			document.getElementById(textId).blur();	
	}	
}
